<h2>Категория</h2> <br/>
<div class="scrollable" >
	<div class="tabulation" data-id="tabs-1">
		<? $tab_num = 1; ?>
		<a class="element active" data-id="tab-<?=$tab_num++?>">Основное</a>
		<? foreach( $this->langs as $l => $lang ): ?>
			<a class="element" data-id="tab-<?=$tab_num++?>"><?=Yii::t('lang',$lang)?></a>
		<?php endforeach; ?>
		<a class="element" data-id="tab-<?=$tab_num++?>">Изображения</a>
	</div>

	<? $form_action = CHtml::normalizeUrl( array('category/saveCategory/id/'. (isset($cat) ? $cat->id : 'new') )); ?>
	<form method="post" action="<?=$form_action?>" class="modal">
		<? if( isset($parent_id) ): ?>
			<input type="hidden" name="cat[parent_id]" value="<?=$parent_id?>">
		<? endif; ?>

		<div class="tabs" id="tabs-1">
			<? $tab_num = 1; ?>
			<div class="tab active" id="tab-<?=$tab_num++?>">
				<table class="step-right">
					<tr>
						<td><strong>Порядок:</strong></td>
						<td><input class="input" name="cat[order]" type="text" value="<?=isset($cat) ? $cat->order : (isset($last_order)) ? $last_order : '1'?>"></td>
					</tr>
					<tr>
						<td><strong>Идентификатор в 1С:</strong></td>
						<td><input class="input" name="cat[id_1c]" type="text" value="<?=isset($cat)?$cat->id_1c:''?>"></td>
					</tr>
					<tr>
						<td><strong>Что будет находиться внутри категории:</strong></td>
						<td>
							<select class="select-btn" name="cat[sub_items_type]">
								<option value="c"><?=Yii::t('cat','subcategories')?></option>
								<option value="i"><?=Yii::t('cat','items')?></option>
							</select>
						</td>
					</tr>
				</table>
			</div>
			<? foreach( $this->langs as $l => $lang ):
				if( $l === Yii::app()->params['defaultLanguage'] ) {
					$suffix = '';
				} else {
					$suffix = '_'.$l;
				}
				if( isset($cat) ) {
					$title_field = 'title'.$suffix;
					$title = $cat->$title_field;

				}
			?>
				<div class="tab" id="tab-<?=$tab_num++?>">
					<table class="step-right">
						<tr>
							<td><strong>Название:</strong></td>
							<td><input class="input wide" name="cat[title<?=$suffix?>]" type="text" id="name" value="<?=isset($cat)?$title:''?>"></td>
						</tr>
					</table>
				</div>
			<?php endforeach; ?>

			<div class="tab" id="tab-<?=$tab_num++?>">
				<div class="step-right">
					<? if( isset($cat->img) ): ?>
						<strong>Изображения:</strong>
						<div class="images images-sortable">
							<div class="image">
								<div class="icon100"><img src="<?= $this->baseUrl . $cat->img ?>" class="round100">
								</div>
								<div class="image-remove">
									<a class="btn-red delete-image" data-id="<?= $cat->id ?>">Удалить</a>
								</div>
								<input type="hidden" name="images[<?= $cat->id ?>][is_deleted]" value="0" id="d-<?= $cat->id ?>">
							</div>
						</div>
					<? endif ?>

					<h4>Добавить изображения:</h4>
					<div class="image-drop dropzone"></div>
					<input class="input" name="cat[img]" type="hidden" id="img" value="<?=isset($cat->img)?$this->baseUrl . $cat->img :''?>">
					<input class="input" name="imgs_url" type="hidden" id="imgs_url" required>
				</div>
			</div>
			<input type="submit" value="Сохранить" class="btn-green save-btn" id="submit">
		</div>
	</form>
</div>
	<script type="text/javascript">
		$(function() {
			var num = 0;
			// табы
			$('.tabulation .element').click(function() {
				$('.tabulation .element').removeClass('active');
				$(this).addClass('active');
				$('#' + $(this).parent().data('id') + ' > .tab').hide();
				$('#' + $(this).data('id')).show();
			});
		});
		$('.delete-image').click(function() {
			$("#d-" + $(this).data('id')).val(1);
			$(this).parent().parent().fadeOut(400);
		});
		var newFileNames = {};
		$('div.image-drop').dropzone({
			url: '<?=CHtml::normalizeUrl(array('function/uploadIcon', 'f'=>'category'))?>',
			addRemoveLinks: true,
			uploadMultiple : false,
			success: function(data, response) {
				$('#img').val(response);
				$('#imgs_url').val($('#imgs_url').val() + ',' + response);
				newFileNames[data.name] = response;
			},
			maxFilesize : 3,
			init: function() {
				var dz = this
				this.on('removedfile', function(data) {
					delete newFileNames[data.name];
					$('#img_urls').val('');
					f = $('#img').val();
					for (var key in newFileNames) {
						$('#img_urls').val($('#img_urls').val() + ',' + newFileNames[key]);
						$('#img_url').val(newFileNames[key]);
					}
					if (f == $('#img').val()) $('#img_url').val('');
				});
			}
		});

		// $("div.images-drop").dropzone();
	</script>
